<%@ page language="java" import="weaver.common.DateUtil" pageEncoding="UTF-8" %>
<%@ page import="weaver.conn.RecordSet" %>
<%@ page import="weaver.conn.RecordSetDataSource" %>
<%@ page import="weaver.formmode.setup.ModeRightInfo" %>
<%@ page import="weaver.general.TimeUtil" %>
<%@ page import="weaver.general.Util" %>
<%@ page import="weaver.hrm.HrmUserVarify" %>
<%@ page import="weaver.hrm.User" %>
<%@ page import="java.util.HashMap" %>
<%@ page import="java.util.Map" %>
<%@ page import="java.util.Set" %>
<%@ page import="java.util.UUID" %>
<script>
</script>
<%
    User user = HrmUserVarify.getUser(request, response);
    int userid = user.getUID();
    int formmodeid= 137;
    RecordSet rs = new RecordSet();
    RecordSetDataSource dataSource = new RecordSetDataSource( "xfcs" );
    RecordSetDataSource dataSource1 = new RecordSetDataSource( "xfcs" );
    String curdate = DateUtil.getCurrentDate();
    out.print( ">>>>>>>>开始执行>>>>>>"+"<br/>");
    try {
        rs.executeQuery( "select zclb,zcbhguid from uf_zclb where tbrq=? group by zclb,zcbhguid",curdate );
        while (rs.next()){
           String zclb = Util.null2String( rs.getString( "zclb" ));
           String zcbhguid = Util.null2String( rs.getString( "zcbhguid" ));
            out.print( ">>>>>>>>资产类别："+zclb+",资产编号："+zcbhguid+"<br/>");

            dataSource.executeSql("select t.RowGuid,t.AssctID,t1.AssctSumName,t.Price,t.ShiYongRenDisplayName,t.ReceiveDeptName,t.Status,t1.RegisterDisplayName,\n" +
                    "LEFT(CONVERT(varchar(100),t1.RegisteTime, 25),16) as inRegisteTime,t1.WuPinLY,t1.PinPai,t1.XingHao,t1.GuiGe,t1.DengJiFS as indjfs,\n" +
                    "t1.BuyerDisplayName,t1.Price,t1.BuyerOuName, LEFT(CONVERT(varchar(100),t1.BuyTime, 25),16) as BuyTime,t1.CaiGouDisplayName,\n" +
                    "LEFT( CONVERT(varchar(100),t1.CaiGouTime, 25),16) as CaiGouTime,t1.BuyFromCompany,t1.Count,t3.OUName as SSDW,t1.Memo as inMemo,\n" +
                    "LEFT(CONVERT(varchar(100),t2.RegisteTime, 25),16) as outRegisteTime,t2.DengJiFS as outdjfs,t2.TakerUserDisplayName,\n" +
                    "t2.TakerOuName,LEFT(CONVERT(varchar(100),t2.TakeTime, 25),16) as TakeTime,t2.ShiYongRenDisplayName,\n" +
                    "t2.ReceiveDeptName,t2.InAddress,t2.PublicAddress,t2.WaiDanWei,t2.fujian,t4.OUName as JSBM,t.Memo as outMemo \n" +
                    "from Misc_Assct_Detail t \n" +
                    "left join  Misc_Assct_RegisteIn t1 on t.RegistInGuid=t1.RowGuid\n" +
                    "left join Misc_Assct_RegisteOut t2 on t.RegistOutGuid=t2.RowGuid \n" +
                    "left join frame_ou t3 on t.SSDW = t3.OUGuid\n" +
                    "left join frame_ou t4 on t.JSBM = t4.OUGuid\n" +
                    "where t.AssctSumGuid='"+zcbhguid+"'");
            int count = 0;
            int counts = dataSource.getCounts();
            out.print( ">>>>>>>>查询记录总数为："+counts+"<br/>");
            while (dataSource.next()){
                Map dataMap = new HashMap(  );
                String RowGuid = Util.null2String(dataSource.getString("RowGuid"));
                String AssctID = Util.null2String(dataSource.getString("AssctID"));
                dataMap.put("zcbh",AssctID);//资产编号
                dataMap.put("rksj",Util.null2String(dataSource.getString("inRegisteTime")));//入库时间
                dataMap.put("djr",Util.null2String(dataSource.getString("RegisterDisplayName")));//登记人
                String AssctSumName = Util.null2String(dataSource.getString("AssctSumName"));
                dataMap.put("zclb",AssctSumName);//资产类别
                dataMap.put("dw",getUnit(AssctSumName));//资产类别
                dataMap.put("zt",Util.null2String(dataSource.getString("Status")));//状态
                dataMap.put("wply",Util.null2String(dataSource.getString("WuPinLY")));//物品来源
                dataMap.put("pp",Util.null2String(dataSource.getString("PinPai")));//品牌
                dataMap.put("xh",Util.null2String(dataSource.getString("XingHao")));//型号
                dataMap.put("gg",Util.null2String(dataSource.getString("GuiGe")));//规格
                String Count = Util.null2String(dataSource.getString("Count"));//数量
                dataMap.put("sl",Count);//数量
                dataMap.put("rkdjfs",Util.null2String(dataSource.getString("indjfs")));//入库登记方式
                dataMap.put("sgr",Util.null2String(dataSource.getString("BuyerDisplayName")));//申购人
                dataMap.put("dj",Util.null2String(dataSource.getString("Price")));//单价
                dataMap.put("sgbm",Util.null2String(dataSource.getString("BuyerOuName")));//申购部门
                dataMap.put("sgsj",Util.null2String(dataSource.getString("BuyTime")));//申购时间
                dataMap.put("cgr",Util.null2String(dataSource.getString("CaiGouDisplayName")));//采购人
                dataMap.put("cgsj",Util.null2String(dataSource.getString("CaiGouTime")));//采购时间
                dataMap.put("gmcs",Util.null2String(dataSource.getString("BuyFromCompany")));//购买厂商
                dataMap.put("szdw",Util.null2String(dataSource.getString("SSDW")));//所属单位
                dataMap.put("rkbz",Util.null2String(dataSource.getString("inMemo")));//入库备注
                dataMap.put("ffsj",Util.null2String(dataSource.getString("outRegisteTime")));//发放时间
                dataMap.put("ckdjfs",Util.null2String(dataSource.getString("outdjfs")));//出库登记方式
                dataMap.put("lyr",Util.null2String(dataSource.getString("TakerUserDisplayName")));//领用人
                dataMap.put("lybm",Util.null2String(dataSource.getString("TakerOuName")));//领用部门
                dataMap.put("lysj",Util.null2String(dataSource.getString("TakeTime")));//领用时间
                dataMap.put("syr",Util.null2String(dataSource.getString("ShiYongRenDisplayName")));//使用人
                dataMap.put("sybm",Util.null2String(dataSource.getString("ReceiveDeptName")));//使用部门
                dataMap.put("szwz",Util.null2String(dataSource.getString("InAddress")));//所在位置
                dataMap.put("ggcs",Util.null2String(dataSource.getString("PublicAddress")));//公共场所
                dataMap.put("wdw",Util.null2String(dataSource.getString("WaiDanWei")));//外单位
                dataMap.put("fj","");//附件
                dataMap.put("jsbm",Util.null2String(dataSource.getString("JSBM")));//结算部门
                dataMap.put("ffbz",Util.null2String(dataSource.getString("outMemo")));//发放备注
                out.print( ">>>>>>>>dataMap："+dataMap.toString()+"<br/>");
                int dataId = insertModelData(formmodeid,userid,dataMap);
                out.print( ">>>>>>>>dataId："+dataId+"<br/>");
                if(dataId>0){
                    count++;
                    dataSource1.executeSql("select CONVERT(varchar(100),a.Modifyime, 23) as Modifyime,a.ShiYongRenDisplayName,a.InAddress,a.Memo,a.ReceiveDeptName from Misc_Assct_Modify a " +
                            "left join  Misc_Assct_Detail_BG b on a.AssctGuid=b.zichanguid where a.AssctGuid='"+RowGuid+"'");
                    int count1 = 0;
                    int counts1 = dataSource1.getCounts();
                    while (dataSource1.next()){
                        String Modifyime = Util.null2String(dataSource1.getString("Modifyime"));
                        String ShiYongRenDisplayName = Util.null2String(dataSource1.getString("ShiYongRenDisplayName"));
                        String InAddress = Util.null2String(dataSource1.getString("InAddress"));
                        String Memo = Util.null2String(dataSource1.getString("Memo"));
                        String ReceiveDeptName = Util.null2String(dataSource1.getString("ReceiveDeptName"));
                        Object[] params = new Object[]{dataId,Modifyime,ShiYongRenDisplayName,ReceiveDeptName,InAddress,Memo};
                        String sql = "insert into uf_zcmxjl_dt1 (mainid,bgsj,syr,sybm,szwz,bz)values(?,?,?,?,?,?)";
                        if(rs.executeUpdate(sql,params)){
                            count1++;
                        }
                    }
                    out.print( "执行成功明细表1数据记录数：>>>"+count1+">>>>>>明细表1总记录数："+counts1);

                    dataSource1.executeSql("select top "+Count+" t.AssctID,t.Status,t.ShiYongRenDisplayName," +
                            "t.ReceiveDeptName,t.InAddress,t.Price,t.Memo from Misc_Assct_Detail t " +
                            "where t.AssctID like '%"+AssctID.substring( 0,8 )+"%' order by t.AssctID");
                    int count2 = 0;
                    int counts2 = dataSource1.getCounts();
                    while (dataSource1.next()){
                        String AssctID1 = Util.null2String(dataSource1.getString("AssctID"));
                        String Status = Util.null2String(dataSource1.getString("Status"));
                        String ShiYongRenDisplayName = Util.null2String(dataSource1.getString("ShiYongRenDisplayName"));
                        String ReceiveDeptName = Util.null2String(dataSource1.getString("ReceiveDeptName"));
                        String InAddress = Util.null2String(dataSource1.getString("InAddress"));
                        String Price = Util.null2String(dataSource1.getString("Price"));
                        String Memo = Util.null2String(dataSource1.getString("Memo"));
                        Object[] params = new Object[]{dataId,AssctID1,Status,ShiYongRenDisplayName,ReceiveDeptName,InAddress,Price,Memo};
                        String sql = "insert into uf_zcmxjl_dt2 (mainid,zcbh,zt,syr,sybm,szwz,dj,bz)values(?,?,?,?,?,?,?,?)";
                        if(rs.executeUpdate(sql,params)){
                            count2++;
                        }
                    }
                    out.print( "执行成功明细表2数据记录数：>>>"+count2+">>>>>>明细表2总记录数："+counts2);
                }
            }
            out.print( "执行成功主表数据记录数：>>>"+count+",主表数据总记录数：>>>"+counts);
        }
    }catch (Exception e){
        out.print("执行异常，请查看日志！！！");
    }
%>
<%!
    //将数据插入表单建模
    public static int insertModelData(int modeid, int userid, Map map){
        int dataid=0;
        RecordSet rs = new RecordSet();
        RecordSet rs1 = new RecordSet();
        //查询该模块表名
        String sql = "select b.tablename,a.formid from modeinfo a left join workflow_bill b on a.formid=b.id where a.id=?";
        rs.executeQuery(sql,modeid);
        rs.next();
        String tablename= Util.null2String(rs.getString("tablename"));
        String uuid = UUID.randomUUID().toString();
        String insSql = "insert into "+tablename
                +"(uuid,modedatacreater,modedatacreatedate,modedatacreatetime,formmodeid)values(?,?,?,?,?)";
        Object[] params = {uuid, userid, TimeUtil.getCurrentDateString(), TimeUtil.getOnlyCurrentTimeString(), modeid};
        if(rs.executeUpdate(insSql,params)){
            rs1.executeQuery("select id from "+tablename+" where uuid=?",uuid);
            rs1.next();
            dataid= Util.getIntValue(rs1.getString("id"));
            if(dataid>0){
                //遍历数据 进行update
                String updatesql="update "+tablename+" set ";
                Set<String> keySet = map.keySet();
                for(String key : keySet){
                    updatesql+=key+"='"+map.get(key).toString()+"',";
                }
                if(updatesql.endsWith(",")){
                    updatesql=updatesql.substring(0, updatesql.length()-1);
                    updatesql+=" where id="+dataid;
                    rs.executeUpdate(updatesql);
                }
                //进行权限重构
                getNewModeRight(userid, modeid, dataid);
            }
        }
        return dataid;
    }
    //建模权限重构
    public static void getNewModeRight(int userid, int formmodeid, int id) {
        ModeRightInfo modeRightInfo = new ModeRightInfo();
        modeRightInfo.setNewRight( true );
        modeRightInfo.editModeDataShare( userid, formmodeid, id  );
    }
    public static String getUnit(String name){
        RecordSet rs = new RecordSet(  );
        rs.executeQuery("select dw from uf_zclxydwdzb where zclx=?",name);
        rs.next();
        String dw = Util.null2String(rs.getString("dw"));
        return dw;
    }
%>